package io.renren.modules.sys.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;

import io.renren.common.constant.Code;
import io.renren.common.exception.BizException;
import io.renren.common.utils.Constant;
import io.renren.modules.sys.dao.RoleDao;
import io.renren.modules.sys.entity.Role;
import io.renren.modules.sys.service.RoleMenuService;
import io.renren.modules.sys.service.RoleService;
import io.renren.modules.sys.service.UserRoleService;
import io.renren.modules.sys.service.UserService;

/**
 * 角色
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2016年9月18日 上午9:45:12
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleDao, Role> implements RoleService {
	@Autowired
	private RoleMenuService sysRoleMenuService;
	@Autowired
	private UserService sysUserService;
    @Autowired
    private UserRoleService sysUserRoleService;

	@Override
	public List<Role> queryPage(Map<String, Object> params) {
		String roleName = (String)params.get("roleName");
		String createUserId = (String)params.get("createUserId");
		if(params.get("page") != null && params.get("limit") != null){
			PageHelper.startPage(Integer.parseInt((String)params.get("page")), Integer.parseInt((String)params.get("limit")));
		}

		List<Role> list = this.selectList(new EntityWrapper<Role>()
				.like(StringUtils.isNotBlank(roleName),"role_name", roleName)
				.eq(createUserId != null,"create_user_id", createUserId));

		return list;
	}

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(Role role) {
        role.setCreateTime(new Date());
        this.insert(role);

        //检查权限是否越权
        checkPrems(role);

        //保存角色与菜单关系
        sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Role role) {
        this.updateAllColumnById(role);

        //检查权限是否越权
        checkPrems(role);

        //更新角色与菜单关系
        sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(String[] roleIds) {
        //删除角色
        this.deleteBatchIds(Arrays.asList(roleIds));

        //删除角色与菜单关联
        sysRoleMenuService.deleteBatch(roleIds);

        //删除角色与用户关联
        sysUserRoleService.deleteBatch(roleIds);
    }


    @Override
	public List<String> queryRoleIdList(String createUserId) {
		return baseMapper.queryRoleIdList(createUserId);
	}

	/**
	 * 检查权限是否越权
	 */
	private void checkPrems(Role role){
		//如果不是超级管理员，则需要判断角色的权限是否超过自己的权限
		if( Constant.SUPER_ADMIN.equals(role.getCreateUserId())){
			return ;
		}
		
		//查询用户所拥有的菜单列表
		List<String> menuIdList = sysUserService.queryAllMenuId(role.getCreateUserId());
		
		//判断是否越权
		if(!menuIdList.containsAll(role.getMenuIdList())){
			throw new BizException(Code.COODE_ROLE_O1,"新增角色的权限，已超出你的权限范围");
		}
	}

	@Override
	public Role saveRole(Role role) {
		
		//是否有上级角色ID
		
		//有查找上级角色
		
		//设置path,无上级ID的角色，路径为当前的角色ID
		
		//保存角色菜单
		
		
		return role;
	}
}
